<template>
  <!-- 布局容器 -->
  <el-row class="container">
    <!-- 顶栏 -->
    <el-col :span="24" class="header">
      <!-- logo区域 -->
      <el-col
        class="logo"
        :class="collapsed ? 'logo-collapse-width' : 'logo-width'"
      >
        {{ collapsed ? sysNameSimple : sysName }}
      </el-col>
      <!-- 折叠小图标 -->
      <el-col :span="1">
        <div class="tools" @click.prevent="collapse">
          <i class="fa fa-align-justify"></i>
        </div>
      </el-col>
      <!-- 顶栏右侧预留 -->
      <el-col :span="2" style="float: right"> 
        欢迎！ 

        <span @click="logout">退出登录</span>
      </el-col>
    </el-col>

    <!-- 主要内容区域 -->
    <el-col :span="24" class="main">
      <!-- 左侧边栏 -->
      <aside :class="collapsed ? 'menu-collapsed' : 'menu-expanded'">
        <!-- 未折叠的导航菜单 -->
        <el-menu
          ref="menu"
          :default-active="$route.path"
          class="el-menu-vertical-demo"
          router
          v-show="!collapsed"
        >
          <!-- 首页单独显示 -->
          <el-menu-item index="/index">
            <i class="el-icon-house"></i>
            <span slot="title">首页</span>
          </el-menu-item>
          <!-- 根据router配置文件循环出菜单 -->
          <template v-for="(item, index) in $router.options.routes">
            <div v-if="!item.hidden" :key="index">
              <el-submenu :index="index + ''" v-if="!item.leaf">
                <template slot="title">
                  <i :class="item.iconCls"></i>
                  <span>{{ item.name }}</span>
                </template>
                <div v-for="child in item.children" :key="child.path">
                  <el-menu-item v-if="!child.hidden" :index="child.path">
                    {{ child.name }}
                  </el-menu-item>
                </div>
              </el-submenu>
            </div>
          </template>
        </el-menu>
        <!-- 折叠的导航菜单 -->
        <ul
          class="el-menu el-menu-vertical-demo collapsed"
          v-show="collapsed"
          ref="menuCollapsed"
        >
          <!-- 首页小图标是单独的 -->
          <li class="el-submenu item">
            <div
              class="el-submenu__title"
              style="padding-left: 20px"
              @click="$router.push('/index')"
            >
              <i class="el-icon-house"></i>
            </div>
          </li>
          <!-- 根据router配置文件循环出菜单，当然不再循环首页了 -->
          <li
            v-for="(item, index) in $router.options.routes.filter(
              (item) => item.name != '首页'
            )"
            :key="index"
            class="el-submenu item"
          >
            <template v-if="!item.hidden">
              <div
                class="el-submenu__title"
                style="padding-left: 20px"
                @mouseover="showMenu(index, true)"
                @mouseout="showMenu(index, false)"
              >
                <i :class="item.iconCls"></i>
              </div>
              <ul
                class="el-menu submenu"
                :class="'submenu-hook-' + index"
                @mouseover="showMenu(index, true)"
                @mouseout="showMenu(index, false)"
              >
                <li
                  v-for="child in item.children"
                  :key="child.path"
                  class="el-menu-item"
                  style="padding-left: 40px"
                  :class="$route.path == child.path ? 'is-active' : ''"
                  @click="$router.push(child.path)"
                >
                  {{ child.name }}
                </li>
              </ul>
            </template>
          </li>
        </ul>
      </aside>

      <!-- 主内部部分 -->
      <section class="content-container">
        <!-- 面板屑导航 -->
        <div class="grid-content bg-purple-light">
          <el-col :span="24" class="breadcrumb-container">
            <el-breadcrumb
              v-if="$router.history.current.name != '首页'"
              separator="/"
              class="breadcrumb-inner"
            >
              <el-breadcrumb-item
                v-for="item in $route.matched"
                :key="item.path"
              >
                {{ item.name }}
              </el-breadcrumb-item>
            </el-breadcrumb>
          </el-col>

          <!-- 路由内嵌页面 -->
          <el-col :span="24" class="content-wrapper">
            <KeepAlive>
              <router-view></router-view>
            </KeepAlive>
          </el-col>
        </div>
      </section>
    </el-col>
  </el-row>
</template>

<script>
import {logout} from '@/api/login'
export default {
  data() {
    return {
      // 系统名称
      sysName: "羊咩咩商城管理系统",
      // 系统简称
      sysNameSimple: "直",
      // 是否折叠
      collapsed: false,
    };
  },
  methods: {
    // 折叠导航栏
    collapse: function () {
      this.collapsed = !this.collapsed;
    },
    // 在折叠模式下，显示菜单
    showMenu(i, status) {
      this.$refs.menuCollapsed.getElementsByClassName(
        "submenu-hook-" + i
      )[0].style.display = status ? "block" : "none";
    },
    logout() {
      logout();

      this.$router.push({
        name: '登录'
      });
    }
  },
};
</script>

<style scoped lang="less">
// 顶部导航条背景颜色
@headerBG: #272b3b;
// 左侧栏背景颜色
@sidebarBG: #f9f9f9;
// 内容Container背景颜色
@contentContainerBG: #f2f5f8;
// 内容Wrapper背景颜色
@contentWrapperBG: white;

.container {
  position: absolute;
  top: 0px;
  bottom: 0px;
  width: 100%;

  // 顶部的样式
  .header {
    height: 60px;
    line-height: 60px;
    background-color: @headerBG;
    color: #fff;

    .logo {
      //width:230px;
      height: 60px;
      font-size: 22px;
      padding-left: 20px;
      padding-right: 20px;
      border-color: rgba(238, 241, 146, 0.3);
      border-right-width: 1px;
      border-right-style: solid;
    }
    .logo-width {
      width: 230px;
    }
    .logo-collapse-width {
      width: 60px;
    }
    .tools {
      padding: 0px 23px;
      width: 14px;
      height: 60px;
      line-height: 60px;
      cursor: pointer;
    }
  }

  // 主题部分样式
  .main {
    display: flex;
    position: absolute;
    top: 60px;
    bottom: 0px;
    overflow: hidden;

    // 侧边栏
    aside {
      flex: 0 0 230px;
      width: 230px;
      .el-menu {
        height: 100%;
        background-color: @sidebarBG;
      }
      .collapsed {
        width: 60px;
        .item {
          position: relative;
        }
        .submenu {
          position: absolute;
          top: 0px;
          left: 60px;
          z-index: 99999;
          height: auto;
          display: none;
        }
      }
      
      .el-menu-item span {
        position: relative;
        top: 2px;
      }
    }
    .menu-collapsed {
      flex: 0 0 60px;
      width: 60px;
    }
    .menu-expanded {
      flex: 0 0 230px;
      width: 230px;
      ul {
        width: 100% !important;
      }
    }

    // 主体内容部分
    .content-container {
      flex: 1;
      overflow-y: scroll;
      padding: 20px;
      background-color: @contentContainerBG;

      .content-wrapper {
        margin-top: 10px;
        background-color: @contentWrapperBG;
        box-sizing: border-box;
        padding: 10px;
      }
    }
  }
}
</style>